//
//  PostingAuxiliary.m
//  CKApp
//
//  Created by CC on 2020/9/12.
//  Copyright © 2020 This Moment. All rights reserved.
//

#import "PostingAuxiliary.h"
#import "LcFoundation.h"

@implementation LocationMeta

- (instancetype)init {
  self = [super init];
  if (self) {
    self.latitude = INVALID_LOCATION;
    self.longitude = INVALID_LOCATION;
  }
  return self;
}

- (BOOL)locationValid {
  return self.latitude != INVALID_LOCATION && self.longitude != INVALID_LOCATION;
}

@end

@implementation CCMediaMeta

- (BOOL)hasUploaded {
  return self.url.length > 0 && self.progress >= 1.0;
}

@end

@implementation PostingAuxiliary

- (NSString*)activityContent {
  return self.activity.length > 0 ? self.activity : @"选择活动";
}

- (NSString *)tagsContent {
  if (self.tags.count > 0) {
    return [self.tags componentsJoinedByString:@"#"];
  }
  return @"添加标签";
}

- (BOOL)mediasHasUploaded {
  for (CCMediaMeta* meta in self.mediaMetas) {
    if (![meta hasUploaded]) {
      return NO;;
    }
  }
  return YES;
}

- (NSArray<PHAsset*>*)assets {
  NSMutableArray* datas = [NSMutableArray arrayWithCapacity:self.mediaMetas.count];
  for (CCMediaMeta* meta in self.mediaMetas) {
    [datas addObject:meta.asset];
  }
  return datas;
}

- (void)deleteMeta:(CCMediaMeta*)mediaMeta; {
  NSMutableArray* copyMetas = self.mediaMetas.mutableCopy;
  for (CCMediaMeta* meta in copyMetas) {
    if ([meta.assetLocalIdentifier isEqualToString:mediaMeta.assetLocalIdentifier]) {
      [copyMetas removeObject:meta];
      self.mediaMetas = copyMetas;
      break;
    }
  }
}

- (NSString*)poiName {
  return self.meta.poiName;
}

@end
